<template>
  <div class="form-container">
    <div class="input-group">
      <label>描述这个变量的用途</label>
      <textarea 
        v-model="description"
        placeholder="例如：存储用户输入的数据，用于后续处理"
        @keyup.enter="handleSubmit"
      ></textarea>
    </div>
    
    <button 
      class="submit-button"
      @click="handleSubmit"
      :disabled="!description.trim() || loading"
    >
      {{ loading ? '生成中...' : '生成变量名建议' }}
    </button>
  </div>
</template>

<script>
import { httpApi } from '@/api'

export default {
  name: 'NameForm',
  data() {
    return {
      description: '',
      loading: false
    }
  },
  methods: {
    async handleSubmit() {
      if (!this.description.trim()) return
      
      this.loading = true
      try {
        // 构造请求参数
        const params = {
          desc: this.description,
        }
        
        // 发送请求到后端
        const res = await httpApi.getSuggestions(params)
        
        // 如果请求成功，触发事件并传递数据
        if (res.data) {
          this.$emit('generate', {
            success: true,
            description: this.description,
            data: res.data
          })
        } else {
          // 如果没有返回数据，使用本地转换
          this.$emit('generate', {
            success: false,
            description: this.description
          })
        }
      } catch (err) {
        console.error('Generate failed:', err)
        // 发生错误时也使用本地转换
        this.$emit('generate', {
          success: false,
          description: this.description
        })
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.form-container {
  background: white;
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
  animation: fadeIn 0.5s ease-out;
}

.input-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  font-size: 1rem;
  color: #4b5563;
  margin-bottom: 0.5rem;
  font-weight: 500;
}

textarea {
  width: 100%;
  min-height: 100px;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: #f8fafc;
  font-size: 0.9375rem;
  color: #1e293b;
  transition: all 0.2s ease;
  resize: vertical;
  font-family: inherit;
}

textarea:focus {
  outline: none;
  border-color: #6366f1;
  background: white;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

textarea::placeholder {
  color: #94a3b8;
}

.submit-button {
  width: 100%;
  padding: 0.875rem;
  background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.submit-button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.2);
}

.submit-button:active:not(:disabled) {
  transform: translateY(0);
}

.submit-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 640px) {
  .form-container {
    padding: 1rem;
  }
  
  textarea {
    min-height: 80px;
  }
  
  .submit-button {
    padding: 0.75rem;
  }
}
</style> 